<?php

namespace App\Http\Controllers\Admin;

use Validator;
use Illuminate\Http\Request;
use App\Model\ShopLabelModel;
use App\Model\AdminLogsModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class ShopLabelController extends Controller
{
    //标签列表
    public function index(Request $request)
    {
        //搜索条件
        $keywords = $request->input('keywords');//关键字
		$sub_state = $request->input('state');//状态

        $pageParam['keywords'] = $keywords;


        $list = ShopLabelModel::when($keywords, function ($query) use ($keywords) {
                return $query->where('name', 'like', "%{$keywords}%");
            })
			->where(function($query){
				if(request()->state){
					$query->where('state',request()->state);
				}
			})
            ->orderBy('id', 'desc')
            ->paginate(10);
        //分页
        $page = $list->appends($pageParam)->links();
        return view('admin.shoplabel.index')
            ->with('keywords',$keywords)
            ->with('session_id',session('admin_id'))
            ->with('page',$page)
			->with('sub_state',$sub_state)
            ->with('list',$list);
    }

    //添加标签页
  	public function add(){
		//类别(搜索)
    	return view('admin.shoplabel.add');
  	}

	//添加标签
	public function insert(Request $request)
	{
		$post = $request->post();
		$validator = Validator::make($post,[
			'name'=>'required|unique:shop_label,name',
			'color'=>'required',
			],[
			'name.required' => '标签名不能为空',
			'name.unique' => '标签品已存在',
			'color.required' => '图片不能为空',
			]);
		if($validator->fails()){
			return returnJson(0, $validator->errors()->first());
		}
		$color = request()->color;
		DB::beginTransaction();
		try {

			$label=new ShopLabelModel;
			$label->name=$request->name;
			$label->color=$color;
			$label->save();

			$msg = '添加标签:'.$request->name;
			AdminLogsModel::addLog($msg);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
		  	throw $e;
		  	return returnJson(0,'操作失败,请稍后再试');
		}

		return returnJson(1,'添加成功');
	}

	 //更改状态
	public function up_state(Request $request,ShopLabelModel $label){
		$state = $request->post('state');//状态
		$id = $request->post('id');

		$returnArr = $label->updateState($id,$state);
		return response()->json($returnArr);
	}

	//编辑页面
	public function edit(){

  	    $list = ShopLabelModel::where('id',request()->id)->first();
  	    return view('admin.shoplabel.edit',compact('list'));
	}

	//编辑商品
	public function update(Request $request){
		$post = $request->post();
    	if(ShopLabelModel::where('id','<>',$post['id'])->where('name',$post['name'])->first()){
    		return returnJson(0,'标签名称已存在');
		}
    	//验证
		$validator = Validator::make($post,[
			'name'=>'required',
			],[
			'name.required' => '标签名不能为空',
			]);
		if($validator->fails()){
			return returnJson(0, $validator->errors()->first());
		}
		$list = ShopLabelModel::where('id',$post['id'])->first();
		if(request()->color){
			$color = request()->color;
		}else{
			$color = '#FF8C19';
		}
		if (ShopLabelModel::where('id', $post['id'])->update(["name" => request()->name,'color'=>$color])) {
			$msg = '编辑标签名【'.$list['name'].'】(id:'.$post['id'].')为:'.'【'.$request->name.'】';
			AdminLogsModel::addLog($msg);
			return returnJson(1, '更改成功');
		} else {
			return returnJson(0, '更改失败');
		}
	}

	//删除商品
	public function del(){
		$id = request()->id;
		//标签信息
		$lable = ShopLabelModel::where('id',$id)->first();
		$msg = '删除标签【'.$lable['name'].'】(id:'.$id.')';
		@unlink(".".$lable['picpath'].$lable['picname']);
		if(ShopLabelModel::where('id',$id)->delete()){
			AdminLogsModel::addLog($msg);
			return returnJson(1, '删除成功');
		}else{
			return returnJson(0, '删除失败');
		}
	}

	//上传标签图片
	public function shop_label_up(Request $request){
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg','gif'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = common_up('shoplabel',$request->file);
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	//编辑图片
	public function edit_mpic(){
		//ID
		$id = request()->id;
		//标签信息
		$list = ShopLabelModel::where('id',$id)->first();
		return view('admin.shoplabel.edit_mpic',compact('list','id'));
	}
	//编辑标签图片
	public function update_mpic(){
		$id = request()->id;
		$color = request()->color;
		DB::beginTransaction();
		try{
			ShopLabelModel::where('id',$id)->update(['color'=>$color]);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
		  	throw $e;
		  	return returnJson(0,'操作失败,请稍后再试');
		}
		return ['code'=>1,'msg'=>'上传成功'];
	}

}
